<template>
  <view class="chat">
    <!-- 机器人消息 -->
    <view class="chat-row left" v-if="is_robot">
      <image class="avatar" :src="img_path" />
      <view class="bubble bot">
        <text class="msg-text" user-select="true">{{ meg }}</text>
        <image v-if="UpImagePath" class="msg-img" :src="UpImagePath" mode="aspectFit" />
      </view>
    </view>
    <!-- 用户消息 -->
    <view class="chat-row right" v-else>
      <view class="bubble self">
        <text class="msg-text" user-select="true">{{ meg }}</text>
        <image v-if="UpImagePath" class="msg-img" :src="UpImagePath" mode="aspectFit" />
      </view>
      <image class="avatar" :src="img_path" />
    </view>
  </view>
</template>

<script>
export default {
  props: {
    img_path: { type: String, default: '../../static/avatar/robot.jpg', required: true },
    meg: { type: String, default: '您好！请问需要我做什么', required: true },
    is_robot: { type: Boolean, default: true, required: true },
    UpImagePath: { type: String, default: null, required: false }
  },
  name: "chat"
}
</script>

<style scoped>
.chat {
  width: 100%;
  display: flex;
  flex-direction: column;
  /* 不要设置 justify-content: space-between，这会拉开消息间距 */
}

.chat-row {
  display: flex;
  align-items: flex-end;
  margin-bottom: 16rpx;
  width: 100%;
  /* 防止内容溢出 */
  overflow: hidden;
}

.chat-row.left {
  justify-content: flex-start;
}

.chat-row.right {
  justify-content: flex-end;
}

.avatar {
  width: 64rpx;
  height: 64rpx;
  border-radius: 50%;
  margin: 0 12rpx;
  box-shadow: 0 0 4rpx #ccc;
  flex-shrink: 0;
}

.bubble {
  max-width: 70%;
  min-width: 60rpx;
  padding: 16rpx 20rpx;
  border-radius: 16rpx;
  font-size: 28rpx;
  word-break: break-all;
  box-shadow: 0 2rpx 8rpx #f0f0f0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  background: #fff;
  /* 防止内容溢出 */
  overflow-wrap: break-word;
  box-sizing: border-box;
}

.bubble.bot {
  background: #f5f6fa;
  color: #333;
  border-top-left-radius: 0;
}

.bubble.self {
  background: #5bb3d9;
  color: #fff;
  border-top-right-radius: 0;
  align-items: flex-end;
}

.msg-text {
  font-size: 28rpx;
  line-height: 1.5;
  word-break: break-all;
}

.msg-img {
  margin-top: 12rpx;
  max-width: 320rpx;
  max-height: 320rpx;
  border-radius: 8rpx;
  box-shadow: 0 0 4rpx #ccc;
  background: #eee;
  display: block;
}
</style>
